package com.guojizhuang.sum;

import com.guojizhuang.writable.MerchantInfoWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class sumReducer extends Reducer<Text, LongWritable, NullWritable, MerchantInfoWritable> {
    protected void reduce(Text key, Iterable<LongWritable> values, Context context) throws IOException, InterruptedException
    {
        long clicks = 0;
        long buys = 0;
        for (LongWritable value : values)
        {
            if(value.get()==0) {
                clicks++;
            } else if(value.get()==1) buys++;
        }
        String[] sells = key.toString().split("\t");
        context.write(NullWritable.get(), new MerchantInfoWritable(Long.parseLong(sells[0]),Long.parseLong(sells[1]), clicks, buys));
    }
}
